$(document).ready(function () {
    console.log(innerWidth)
    console.log(innerHeight)
    $("#outer").css("height", innerHeight * 0.75)
    $('#outer').css("width", innerWidth * 0.8)

    let url = window.location.href;
    let urlObj = new URL(url);
    let params = new URLSearchParams(urlObj.search);

    let stockCode = params.get("code");
    let stockName = params.get("name")
    let twoDecimal = (num) => {
        let multiplied = num * 100;
        let rounded = Math.round(multiplied);
        return rounded / 100
    }
    // stockCode 为空 证明是从predict页面过来的
    if (stockCode === "" || stockCode === undefined || stockCode === null){
        $('#iframe').attr("src", "./line-race.html")
        $('#title').text(`${localStorage.getItem("stockName")}  ${localStorage.getItem("stockCode")}`)
        return
    }
    // 从推荐页过来的 去除 .SH 后缀 匹配后端
    let stockCodeNoSuffix = stockCode.substring(0,6)
    $.ajax({
        url: host + "/api/test",
        type: "get",
        data: {
            stock_code: stockCodeNoSuffix,
        },
        success: function (res) {
            let arr = [] //这个数组存的事K线图的数据
            let trendArr = [] //这个数组存的是趋势图的数据

            trendArr.push(["Income", "Country", "Year"])
            let length = res.data.close.length;
            for (let i = 0; i < length; i++) {
                let arr1 = []
                let arr2 = []
                arr1.push(res.data.trade_date[i])
                arr1.push(twoDecimal(res.data.open[i]))
                arr1.push(twoDecimal(res.data.close[i]))
                arr1.push(twoDecimal(res.data.low[i]))
                arr1.push(twoDecimal(res.data.high[i]))

                arr.push(arr1)

                if (res.data.predict_high[i] !== -1 &&
                    res.data.predict_high[i] !== 0
                ) {
                    let actualHigh = res.data.high[i]
                    let actualLow = res.data.low[i]
                    let predictHigh = res.data.predict_high[i]
                    let predictLow = res.data.predict_low[i]
                    let date = res.data.trade_date[i]

                    trendArr.push([twoDecimal(predictHigh), stockInfo.predictHigh, date])
                    trendArr.push([twoDecimal(predictLow), stockInfo.predictLow, date])
                    trendArr.push([twoDecimal((predictLow + predictHigh) / 2), stockInfo.predict, date])

                    if (i >= res.data.predict_high.length - 3) continue
                    trendArr.push([twoDecimal(actualHigh), stockInfo.actualHigh, date])
                    trendArr.push([twoDecimal(actualLow), stockInfo.actualLow, date])
                    trendArr.push([twoDecimal((actualLow + actualHigh) / 2), stockInfo.actual, date])
                }
            }
            console.log(arr)
            localStorage.setItem("data", JSON.stringify(arr))
            localStorage.setItem("stockCode", res.data.ts_code[0])
            localStorage.setItem("trend", JSON.stringify(trendArr))
            console.log(trendArr)

            $('#iframe').attr("src", "./line-race.html")
            $('#title').text(`${stockName}  ${stockCode}`)
        },
        error: function (res) {
            $("#light").css('display', 'none');
            alert("股票代码有误")
            console.log("error")
            window.location.reload();//刷新原页面，如果出问题刷新页面
        }
    })
})
function changeTrend() {
    $("#dct1").addClass("currents")
    $("#dct2").removeClass("currents")
    $("#iframe").attr("src", "./line-race.html")
}

function changeK() {
    $("#dct1").removeClass("currents")
    $("#dct2").addClass("currents")
    $("#iframe").attr("src", "./candlestick-sh.html")
}

function loadTrend() {
    console.log("loading")
}

function loadTrend() {
    console.log("loading")
}
function backto_index(){
    location.href = `index1.html`;/*当前页面*/
    // window.location.reload();//刷新原页面
}